package com.moli.iov.notify.remote.impl;

import com.alibaba.fastjson.JSONObject;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.notify.config.SmsProperties;
import com.moli.iov.notify.constants.Constant;
import com.moli.iov.notify.enums.SendStatusEnum;
import com.moli.iov.notify.model.dto.MailDto;
import com.moli.iov.notify.model.dto.ResetPwdSmsDto;
import com.moli.iov.notify.model.dto.SmsDto;
import com.moli.iov.notify.model.dto.sms.RestPwdJsonDto;
import com.moli.iov.notify.remote.ISmsRemoteService;
import com.moli.iov.notify.service.IMailService;
import com.moli.iov.notify.service.ISmsService;
import com.moli.iov.notify.util.MailUtil;
import com.moli.iov.notify.util.SendSmsUtil;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;

/**
 * <p>
 * 短信业务
 * </p>
 *
 * @author zql
 * @since 2019/9/11 9:28
 */
@Slf4j
@Service(group = "${service.group}")
public class SmsRemoteServiceImpl implements ISmsRemoteService {

  @Autowired
  private ISmsService notifySmsService;

  @Autowired
  private SmsProperties smsProperties;

  @Autowired
  private SendSmsUtil sendSmsUtil;

  @Autowired
  private MailUtil mailUtil;

  @Autowired
  private IMailService mailService;

  /**
   * 重置密码 发送通知
   * @param resetPwdSmsDto
   * @return
   */
  @Override
  public RestResponse sendSmsOfResetPwd(ResetPwdSmsDto resetPwdSmsDto) {
    try{
      checkSendSmsOfResetPwd(resetPwdSmsDto);

      // 发送邮件
      sendMail(assembleMailDto(resetPwdSmsDto));

      // 发送短信
      return sendSms(assembleSmsDto(resetPwdSmsDto));
    }catch(Exception e){
      log.error("重置密码 发送短信通知", e);
      return RestResponse.failure("发送短信失败");
    }
  }

  /**
   * 发送邮件
   * @param mailDto
   * @return
   */
  private void sendMail(MailDto mailDto) {
    try {
      mailUtil.sendMail(mailDto);

      mailService.saveNotifyMail(mailDto);

    }catch(Exception e){
      log.error("发送邮件 异常", e);
    }
  }

  /**
   * 添加用户发送 组装email数据
   * @param resetPwdSmsDto
   * @return
   */
  private MailDto assembleMailDto(ResetPwdSmsDto resetPwdSmsDto){
    MailDto mailDto = new MailDto();
    mailDto.setTo(resetPwdSmsDto.getEmail());
    mailDto.setSubject(Constant.RESET_USER_MAIL_TITLE);
    String text = Constant.RESET_USER_MAIL_TEXT
        .replaceAll("@1", Constant.IOV_URL);
    text = text.replaceAll("@2", resetPwdSmsDto.getEmail());
    text = text.replaceAll("@3", resetPwdSmsDto.getPassword());
    mailDto.setText(text);

    return mailDto;
  }

  /**
   * 发送短信
   * @param smsDto
   * @return
   */
  @Async
  @Override
  public RestResponse sendSms(SmsDto smsDto) {

    boolean isT = sendSmsUtil.sendSms(smsDto);
    int smsStatus = isT ? SendStatusEnum.SUCCESS.getFlag() : SendStatusEnum.FAIL.getFlag();

    notifySmsService.saveNotifySms(smsDto, smsStatus);

    if(!isT){
      return RestResponse.failure("发送短信失败");
    }
    return RestResponse.success();
  }


  /**
   * 重置密码 发送通知 参数检查
   * @param resetPwdSmsDto
   */
  private void checkSendSmsOfResetPwd(ResetPwdSmsDto resetPwdSmsDto){

    if(StringUtil.isBlank(resetPwdSmsDto.getMobile())){
      throw new BusinessException("手机号不能为空");
    }
    if (StringUtil.isBlank(resetPwdSmsDto.getEmail())) {
      throw new BusinessException("邮箱不能为空");
    }
    if (StringUtil.isBlank(resetPwdSmsDto.getPassword())) {
      throw new BusinessException("密码不能为空");
    }
  }

  /**
   * 重置密码 发送通知 组装 SmsDto
   * @param resetPwdSmsDto
   * @return
   */
  private SmsDto assembleSmsDto(ResetPwdSmsDto resetPwdSmsDto){
    SmsDto smsDto = new SmsDto();
    smsDto.setMobile(resetPwdSmsDto.getMobile());
    smsDto.setTempleteCode(smsProperties.getResetPwdTempleteCode());

    smsDto.setJsonData(JSONObject.toJSONString(new RestPwdJsonDto(resetPwdSmsDto.getEmail())));

    return smsDto;
  }
}
